﻿@model OrdersReportModel

@using Telerik.Web.Mvc.UI

@{  
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}

@using (Html.BeginForm())
{
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.StartDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.EndDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.EndDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.OrderStatusId):
            </td>
            <td class="adminData">
                @Html.DropDownList("OrderStatusId", Model.AvailableOrderStatuses)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PaymentStatusId):
            </td>
            <td class="adminData">
                @Html.DropDownList("PaymentStatusId", Model.AvailablePaymentStatuses)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-customer-orders" class="t-button" value="@T("Admin.Customers.Reports.RunReport")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<OrderReportLineModel>()
                .Name("customer-orders-grid")
                .ClientEvents(events => events.OnDataBinding("customerOrdersOnDataBinding"))
                .Columns(columns =>
                {
                    columns.Bound(x => x.OrderId)
                        .Sortable(false)
                        .Filterable(false);
                    columns.Bound(x => x.CustomerName);
                    columns.Bound(x => x.OrderStatus);
                    columns.Bound(x => x.PaymentStatus);
                    columns.Bound(x => x.OrderTotal);
                    columns.Bound(x => x.CreatedOn).Template(x => x.CreatedOnStr).ClientTemplate("<#= CreatedOnStr #>");
                })
                .DataBinding(dataBinding => dataBinding.Ajax().Select("ReportCustomerOrdersList", "Customer"))
                .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                .Sortable()
                .Filterable()
                .EnableCustomBinding(true))
            </td>
        </tr>
    </table>
    
    <script type="text/javascript">

        $(document).ready(function () {

            $('#search-customer-orders').click(function () {
                var grid = $('#customer-orders-grid').data('tGrid');
                grid.currentPage = 1; //new search. Set page size to 1
                grid.ajaxRequest();
                return false;
            });

        });

    function customerOrdersOnDataBinding(e) {
        var searchModel = {
                StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
                EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
                OrderStatusId: $('#@Html.FieldIdFor(model => model.OrderStatusId)').val(),
                PaymentStatusId: $('#@Html.FieldIdFor(model => model.PaymentStatusId)').val()
                };
        e.data = searchModel;
    }

    </script>
    
}